#define _CRT_SECURE_NO_WARNINGS 1
#include<vector>
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
    
};

vector<vector<int>> levelOrder(TreeNode* root) {
    vector<vector<int>> vv;
    size_t levelsize = 1;
    queue<TreeNode*> q;
    if (root)
    {
        q.push(root);
    }

    while (!q.empty())
    {
        vector<int> v;
        while (levelsize--)
        {
            TreeNode* front = q.front();
            q.pop();
            v.push_back(front->val);
            if (front->left)
                q.push(front->left);
            if (front->right)
                q.push(front->right);
        }
        levelsize = q.size();
        vv.push_back(v);
    }
    return vv;
}

class MinStack {
public:
    MinStack() {
    }

    void push(int val) {
        st.push(val);
        if (minst.empty() || minst.top() >= val)
            minst.push(val);
    }

    void pop() {
        int val = st.top();
        st.pop();
        if (val == minst.top())
            minst.pop();
    }

    int top() {
        return st.top();
    }

    int getMin() {
        return minst.top();
    }
private:
    stack<int> st;
    stack<int> minst;
};

bool IsPopOrder(vector<int>& pushV, vector<int>& popV) {
    stack<int> st;
    int i = 0;
    for (auto e : pushV)
    {
        st.push(e);
        while (!st.empty() && popV[i] == st.top())
        {
            st.pop();
            i++;
        }
    }
    if (!st.empty())
        return false;
    return true;
}

struct ListNode {
    int val;
    ListNode* next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode* next) : val(x), next(next) {}
    
};

ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
    if (!list1)
        return list2;
    if (!list2)
        return list1;

    if (list1->val <= list2->val)
    {
        list1->next = mergeTwoLists(list1->next, list2);
        return list1;
    }
    else
    {
        list2->next = mergeTwoLists(list2->next, list1);
        return list2;
    }
}